package com.tccmeta.httpApi;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tccmeta.po.Meta;
import com.tccmeta.po.MetaRelation;
import com.tccmeta.servlce.MetaService;
import com.tccmeta.httpApi.vo.MetaRequest;
import lombok.extern.java.Log;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.script.ScriptException;

/**
 * @author pan
 * WEB-RESTFUL API
 */
@RestController
@RequestMapping("/meta")
@Log
public class MetaController {
    @Resource
    MetaService metaService;

    /**
     * 如果不传子查所有，实体和卷曲的不存在relation
     * @param request 节点
     * @return 分页数据
     */
    @PostMapping("/node/query")
    public ResponseEntity<Page<Meta>> query(@RequestBody MetaRequest request) {
        Page<Meta> result = metaService.findPage(request.getPage(),request.getNode());
        return ResponseEntity.ok(result);
    }

    /**
     * 节点保存
     * @param node 节点保存
     * @throws ScriptException 脚本执行错误
     */
    @PostMapping("/node/save")
    public Meta save(@RequestBody Meta node) throws ScriptException {
        return metaService.save(node);
    }

    /**
     * 节点删除
     * @param node 节点
     */
    @PostMapping("/node/delete")
    public void delete(@RequestBody Meta node) {
        metaService.delete(node);
    }

    /**
     * 关联关系保存
     * @param relation 关联关系
     */
    @PostMapping("/relation/save")
    public void saveRelation(@RequestBody MetaRelation relation) {
        metaService.saveRelation(relation.getMetaId(),relation.getRelatedMetaId());
    }

    /**
     * 关联关系删除
     * @param relation 关联关系
     */
    @PostMapping("/relation/delete")
    public void deleteRelation(@RequestBody MetaRelation relation) {
        metaService.deleteRelation(relation.getMetaId(),relation.getRelatedMetaId());
    }
}
